//
//  RLNearByController.m
//  RelaxLinkProduct
//
//  Created by relax on 2017/11/26.
//  Copyright © 2017年 relax. All rights reserved.
//

#import "RLNearByController.h"
#import "RLNearbyFlowLayout.h"
#import "RLNearyByCell.h"

@interface RLNearByController () <UICollectionViewDelegate , UICollectionViewDataSource>

@end

static NSString *const reuserNearbyCellID = @"reuserNearbyCellID";

@implementation RLNearByController {
    /** 附近的人 */
    UICollectionView *_nearbyView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor grayColor];
    
    [self prepareNearbyCollectionView];
}

#pragma mark - 准备 collectionView
- (void)prepareNearbyCollectionView {
    RLNearbyFlowLayout *flowLayout = [[RLNearbyFlowLayout alloc] init];
    _nearbyView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _nearbyView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:_nearbyView];
    
    _nearbyView.delegate = self;
    _nearbyView.dataSource = self;
    
    [_nearbyView registerClass:[RLNearyByCell class] forCellWithReuseIdentifier:reuserNearbyCellID];
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return 10;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    RLNearyByCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuserNearbyCellID forIndexPath:indexPath];
    
    // cell.backgroundColor = [UIColor orangeColor];
    // 默认隐藏
    cell.alpha = 0.01;
    
    return cell;
    
}

//- (void)viewWillAppear:(BOOL)animated {
//    [super viewWillAppear:animated];
//    
//    [self animationCells];
//}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    // [self animationCells];
}

#pragma mark - 给 cell 添加动画
- (void)animationCells {
    NSArray<UICollectionViewCell *> *cells = _nearbyView.visibleCells;
    for (UICollectionViewCell *cell in cells) {
        cell.transform = CGAffineTransformMakeScale(0.1, 0.1);
        cell.alpha = 0.1;
        [UIView animateWithDuration:.8 animations:^{
            cell.transform = CGAffineTransformIdentity;
            cell.alpha = 1.0;
        }];
    }
}

//- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
//    // 给 cell 加一个动画
//    // UICollectionViewCell *cell = [collectionView cellForItemAtIndexPath:indexPath];
//    cell.transform = CGAffineTransformMakeScale(0.1, 0.1);
//    cell.alpha = 0.1;
//    [UIView animateWithDuration:0.25 animations:^{
//        cell.transform = CGAffineTransformIdentity;
//        cell.alpha = 1.0;
//    }];
//}

@end
